/******************************************************************************
 * Spine Runtimes Software License
 * Version 2.3
 * 
 * Copyright (c) 2013-2015, Esoteric Software
 * All rights reserved.
 * 
 * You are granted a perpetual, non-exclusive, non-sublicensable and
 * non-transferable license to use, install, execute and perform the Spine
 * Runtimes Software (the "Software") and derivative works solely for personal
 * or internal use. Without the written permission of Esoteric Software (see
 * Section 2 of the Spine Software License Agreement), you may not (a) modify,
 * translate, adapt or otherwise create derivative works, improvements of the
 * Software or develop new applications using the Software or (b) remove,
 * delete, alter or obscure any trademarks or any copyright, trademark, patent
 * or other intellectual property or proprietary rights notices on or in the
 * Software, including any copy thereof. Redistributions in binary or source
 * form must include this license and terms.
 * 
 * THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE "AS IS" AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
 * EVENT SHALL ESOTERIC SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *****************************************************************************/
using UnityEngine;
using System.Collections.Generic;

namespace Spine.Unity {
	[ExecuteInEditMode]
	public class BoundingBoxFollower : MonoBehaviour {
		
		public SkeletonRenderer skeletonRenderer;

		[SpineSlot(dataField: "skeletonRenderer", containsBoundingBoxes: true)]
		public string slotName;

		Slot slot;
		BoundingBoxAttachment currentAttachment;
		PolygonCollider2D currentCollider;
		string currentAttachmentName;
		bool valid = false;
		bool hasReset;

		public Dictionary<BoundingBoxAttachment, PolygonCollider2D> colliderTable = new Dictionary<BoundingBoxAttachment, PolygonCollider2D>();
		public Dictionary<BoundingBoxAttachment, string> attachmentNameTable = new Dictionary<BoundingBoxAttachment, string>();

		public string CurrentAttachmentName { get { return currentAttachmentName; } }
		public BoundingBoxAttachment CurrentAttachment { get { return currentAttachment; } }
		public PolygonCollider2D CurrentCollider { get { return currentCollider; } }
		public Slot Slot { get { return slot; } }

		void OnEnable () {
			ClearColliders();

			if (skeletonRenderer == null)
				skeletonRenderer = GetComponentInParent<SkeletonRenderer>();

			if (skeletonRenderer != null) {
				skeletonRenderer.OnRebuild -= HandleReset;
				skeletonRenderer.OnRebuild += HandleReset;

				if (hasReset)
					HandleReset(skeletonRenderer);
			}
		}

		void OnDisable () {
			skeletonRenderer.OnRebuild -= HandleReset;
		}

		void Start () {
			if (!hasReset && skeletonRenderer != null)
				HandleReset(skeletonRenderer);
		}

		public void HandleReset (SkeletonRenderer renderer) {
			if (string.IsNullOrEmpty(slotName))
				return;

			hasReset = true;

			ClearColliders();
			colliderTable.Clear();

			if (skeletonRenderer.skeleton == null) {
				skeletonRenderer.OnRebuild -= HandleReset;
				skeletonRenderer.Initialize(false);
				skeletonRenderer.OnRebuild += HandleReset;
			}

			var skeleton = skeletonRenderer.skeleton;
			slot = skeleton.FindSlot(slotName);
			int slotIndex = skeleton.FindSlotIndex(slotName);

			foreach (var skin in skeleton.Data.Skins) {
				var attachmentNames = new List<string>();
				skin.FindNamesForSlot(slotIndex, attachmentNames);

				foreach (var attachmentName in attachmentNames) {
					var attachment = skin.GetAttachment(slotIndex, attachmentName);
					var boundingBoxAttachment = attachment as BoundingBoxAttachment;
					if (boundingBoxAttachment != null) {
						var bbCollider = SkeletonUtility.AddBoundingBoxAsComponent(boundingBoxAttachment, gameObject, true);
						bbCollider.enabled = false;
						bbCollider.hideFlags = HideFlags.HideInInspector;
						colliderTable.Add(boundingBoxAttachment, bbCollider);
						attachmentNameTable.Add(boundingBoxAttachment, attachmentName);
					}
				}
			}

			valid = colliderTable.Count != 0;

			if (!valid)
				Debug.LogWarning("Bounding Box Follower not valid! Slot [" + slotName + "] does not contain any Bounding Box Attachments!");
		}

		void ClearColliders () {
			var colliders = GetComponents<PolygonCollider2D>();
			if (Application.isPlaying) {
				foreach (var c in colliders)
					Destroy(c);
			} else {
				foreach (var c in colliders)
					DestroyImmediate(c);
			}

			colliderTable.Clear();
			attachmentNameTable.Clear();
		}

		void LateUpdate () {
			if (!skeletonRenderer.valid)
				return;

			if (slot != null) {
				if (slot.Attachment != currentAttachment)
					SetCurrent((BoundingBoxAttachment)slot.Attachment);
			}
		}

		void SetCurrent (BoundingBoxAttachment attachment) {
			if (currentCollider)
				currentCollider.enabled = false;

			if (attachment != null) {
				currentCollider = colliderTable[attachment];
				currentCollider.enabled = true;
			} else {
				currentCollider = null;
			}

			currentAttachment = attachment;
			currentAttachmentName = currentAttachment == null ? null : attachmentNameTable[attachment];
		}
	}

}
